#!/bin/bash
# https://github.com/surajx/qemu-arm-linux/blob/8ce0511fec822d03634bbad51e4f2adc8c2f0474/README.md
# https://github.com/BarretRen/ProgrammingNotes/blob/099bf1da8a1e62d422df4858323fe36dda701583/src/%E5%B5%8C%E5%85%A5%E5%BC%8F/%E5%9F%BA%E4%BA%8EQEMU%E7%9A%84ARM%E4%BB%BF%E7%9C%9F.md
# https://github.com/jasonblog/note/blob/91faab7e66309f8b16776039139a2d6ceb171a2c/qemu/qemu_driver.md
# https://github.com/apachiww/Notes/blob/3bbe82bc1160fa67f993444ed9cce8c324647e62/200908b_qemu-arm.md

# -a if file exists, retrun true
# -d if directory exists, return true
# -e if file of directory exists, return true
# git clone git://git.busybox.net/busybox --depth=10

# 1. 在 $1 目录下创建 rootfs，用于保存文件系统所需要的文件
# 2. 在 $1 目录下创建 qemu, 保存内核镜像, 目录树
# 3. 在 $1 目录下创建 tmpfs, 用于挂载镜像文件

if [ $# -ne 1 ]; then 
    echo "Please set correct path!"
    return
fi


if [ ! -d $1/busybox/ ]; then
    git clone git://git.busybox.net/busybox --depth=1 $1/busybox
    return
fi

if [ -d $1/busybox/_install ]; then
    echo "BusyBox has been compiled!"
else
    echo "Please compile busybox 
        export ARCH=arm 
        export CROSS_COMPILE=arm-linux-gnueabihf-
        make menuconfig
        # 勾选 Setting-> Build Options-> [*] Build static binary (no shared libs)
        # 因为模拟环境下无法使用动态库方式
        make 
        make install
        "
    return
fi


if [ -d $1/rootfs ]; then
    rm -rf $1/rootfs
fi

if [ -d $1/qemu/tmpfs ]; then
    rm -rf $1/qemu/tmpfs
else
    mkdir -p $1/qemu/tmpfs
fi

cp -r $1/busybox/_install $1/rootfs

mkdir -p $1/rootfs/lib
sudo cp -arf /usr/arm-linux-gnueabihf/lib/*so*  $1/rootfs/lib


mkdir -p $1/rootfs/dev
mkdir -p $1/rootfs/proc
mkdir -p $1/rootfs/mnt
mkdir -p $1/rootfs/sys
mkdir -p $1/rootfs/tmp
mkdir -p $1/rootfs/root
mkdir -p $1/rootfs/etc
mkdir -p $1/rootfs/etc/init.d

sudo mknod $1/rootfs/dev/tty1 c 4 1
sudo mknod $1/rootfs/dev/tty2 c 4 2
sudo mknod $1/rootfs/dev/tty3 c 4 3
sudo mknod $1/rootfs/dev/tty4 c 4 4
sudo mknod $1/rootfs/dev/console c 5 1
sudo mknod $1/rootfs/dev/null c 1 3

# 创建fstab
echo "#Device	mountpoint	type	option	dump	fsckorder" > $1/rootfs/etc/fstab
echo "proc	/proc	proc	defaults	0	0" >> $1/rootfs/etc/fstab
echo "temps	/tmp	proc	defaults	0	0" >> $1/rootfs/etc/fstab
echo "none	/tmp	ramfs	defaults	0	0" >> $1/rootfs/etc/fstab
echo "sysfs	/sys	sysfs	defaults	0	0" >> $1/rootfs/etc/fstab
echo "mdev	/dev	ramfs	defaults	0	0" >> $1/rootfs/etc/fstab

# 创建inittab
echo "::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
tty2::askfirst:/bin/sh
tty3::askfirst:/bin/sh
tty4::askfirst:/bin/sh " > $1/rootfs/etc/initab

# 创建init脚本
echo "mount -a
echo '/sbin/mdev' > /proc/sys/kernel/hotplug
/sbin/mdev -s
mount -a " > $1/rootfs/etc/init.d/rcS
sudo chmod 777 $1/rootfs/etc/init.d/rcS


# 制作镜像文件
dd if=/dev/zero of=$1/qemu/rootfs.ext3 bs=1M count=32
mkfs.ext3 $1/qemu/rootfs.ext3

sudo mount -o loop $1/qemu/rootfs.ext3 $1/qemu/tmpfs   
sudo cp -rf $1/rootfs/* $1/qemu/tmpfs
sudo umount $1/qemu/tmpfs

# 启动
qemu-system-arm -M vexpress-a9 -m 512M -kernel $1/qemu/zImage \
-dtb $1/qemu/vexpress-v2p-ca9.dtb -nographic \
-append "root=/dev/mmcblk0 rw console=ttyAMA0" -sd $1/qemu/rootfs.ext3
